var unitListUrl = "/GetUnitListServlet";
var CateGoryListUrl = "/GetCategoryListServlet";
var addGoodsUrl = "/AddGoodsServlet";
var GetMaxIdUrl = "/GetMaxGoodsIdServlet";

$(function () {
    //商品类别下拉列表
    goodsCateGory.initList();
    //商品单位下拉列表
    goodsUnit.initList();
    goodsManage.initGoodsId();
})

var goodsManage = {};
var goodsCateGory = {};
var goodsUnit = {};

/**
 * 商品单位下拉框
 */
goodsUnit.initList = function(){
    $.ajax({
        url: unitListUrl,
        type: 'get',
        dataType: 'json',
        success: function (result) {
            if (result.status > 0){
                var res = result.data;
                for(var i=0;i<res.length;i++){
                    var opt = $("<option value='"+res[i].id+"'>"+res[i].name+"</option>");
                    $("#unitId").append(opt)
                }
            }
        }
    })
}

/**
 * 类别下拉框
 */
goodsCateGory.initList = function(){
    $.ajax({
        url: CateGoryListUrl,
        type: 'get',
        dataType: 'json',
        success: function (result) {
            if (result.status > 0 ) {
                var res = result.data;
                for (var i = 0; i < res.length; i++) {
                    var opt = $("<option value='" + res[i].id + "'>" + res[i].name + "</option>");
                    $("#categoryId").append(opt);
                }
            }
        }
    })
}

goodsManage.initGoodsId = function(){
    $.ajax({
        url: GetMaxIdUrl,
        type: 'get',
        dataType: 'json',
        success: function (result) {
            if (result.status > 0) {
                console.log(result.data);
                var res = result.data;
                var id = parseInt(res[0].goodsId);
                $("#goodsId").val(id);
            }
        }
    })
}

/**
 * 增加商品
 */
goodsManage.addGoods = function () {
    $.ajax({
        url: addGoodsUrl,// 自己完成后台
        type: 'post',
        data: {
            "goodsId": $("#goodsId").val(),
            "name": $("#goodsName").val(),
            "code": $("#code").val(),
            "type": $("#type").val(),
            "unitId": $("#unitId").val(),
            "price": $("#price").val(),
            "categoryId": $("#categoryId").val()
        },
        dataType: 'json',
        success: function (result) {
            if (result.status > 0) {
                toastr['success']("操作成功");
                var a = parseInt($("#goodsId").val());
                $("#goodsId").val(a+1),
                $("#goodsName").val(null),
                $("#code").val(null),
                $("#type option[value='0']").attr("selected","selected"),
                $("#unitId option[value='0']").attr("selected","selected"),
                $("#price").val(null),
                $("#categoryIdoption[value='0']").attr("selected","selected")
            } else {
                toastr['error']("操作失败");
            }
        }

    })
}

/**
 * 重置按钮
 */
goodsManage.resetGoods = function () {
    var a = parseInt($("#goodsId").val());
    $("#goodsId").val(a),
    $("#goodsName").val(null),
    $("#code").val(null),
    $("#type option[value='0']").attr("selected","selected"),
    $("#unitId option[value='0']").attr("selected","selected"),
    $("#price").val(null),
    $("#categoryIdoption[value='0']").attr("selected","selected")
}